package org.x.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.x.common.base.util.UserContextHolder;
import org.x.common.web.exception.BusinessException;
import org.x.user.mapper.SysDeptMapper;
import java.lang.Boolean;
import java.lang.Override;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.x.user.entity.SysDept;
import org.x.user.dto.DeptDTO;
import org.x.user.query.DeptQuery;
import org.x.user.service.DeptService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.Date;
import java.util.List;

import static org.x.common.base.result.ResultCode.DEPT_EXIST;
import static org.x.common.base.result.ResultCode.DEPT_NOT_FOUND;

/**
 * 组织架构管理的实现
 *
 * @author 夏包包
 * @date 2025-03-08 13:39:03
 */
@Service
public class DeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements DeptService {

    @Autowired
    private SysDeptMapper deptMapper;

    @Override
    public List<SysDept> getDeptList(DeptDTO deptDTO) {

        QueryWrapper<SysDept> queryWrapper = Wrappers.query();
        queryWrapper.eq("tenant_id", UserContextHolder.getTenantId());
        return deptMapper.selectList(queryWrapper);
    }

    @Override
    public Boolean addDept(DeptDTO deptDTO) {
        QueryWrapper<SysDept> queryWrapper = Wrappers.query();
        queryWrapper.eq("dept_name", deptDTO.getDeptName()).eq("tenant_id", deptDTO.getTenantId());
        // 查询部门信息是否存在
        SysDept deptDO = deptMapper.selectOne(queryWrapper);
        if (deptDO != null) {
            // 部门信息已存在，返回失败信息
            throw new BusinessException(DEPT_EXIST);
        }
        // 保存部门信息
        SysDept newDeptDO = new SysDept();
        newDeptDO.setId(deptDTO.getDeptId());
        newDeptDO.setDeptName(deptDTO.getDeptName());
        newDeptDO.setParentId(deptDTO.getParentId());
        newDeptDO.setDeptDesc(deptDTO.getDeptDesc());
        newDeptDO.setTenantId(deptDTO.getTenantId());
        newDeptDO.setDeptCode(deptDTO.getDeptCode());
        newDeptDO.setDataScopeRule(deptDTO.getDataScopeRule());
        newDeptDO.setSort(deptDTO.getSort());
        // 假设创建人是admin
        newDeptDO.setCreateBy("admin");
        newDeptDO.setCreateTime(new Date());
        int result = deptMapper.insert(newDeptDO);
        return result > 0;
    }

    @Override
    public Boolean deleteDept(DeptDTO deptDTO) {
        // 查询部门信息是否存在
        SysDept deptDO = deptMapper.selectById(deptDTO.getDeptId());
        if (deptDO == null) {
            // 部门信息不存在，返回失败信息
            throw new BusinessException(DEPT_NOT_FOUND);
        }
        // 删除部门及其子部门信息
        QueryWrapper<SysDept> queryWrapper = Wrappers.query();
        queryWrapper.eq("parent_id", deptDO.getId()).or().eq("id", deptDO.getId());
        int result = deptMapper.delete(queryWrapper);
        return result > 0;
    }

    @Override
    public Boolean updateDept(DeptDTO deptDTO) {
        // 查询部门信息是否存在
        SysDept deptDO = deptMapper.selectById(deptDTO.getDeptId());
        if (deptDO == null) {
            // 部门信息不存在，返回失败信息
            throw new BusinessException(DEPT_NOT_FOUND);
        }
        // 更新部门信息
        SysDept updatedDeptDO = new SysDept();
        updatedDeptDO.setId(deptDTO.getDeptId());
        updatedDeptDO.setDeptName(deptDTO.getDeptName());
        updatedDeptDO.setParentId(deptDTO.getParentId());
        updatedDeptDO.setDeptDesc(deptDTO.getDeptDesc());
        updatedDeptDO.setDeptCode(deptDTO.getDeptCode());
        updatedDeptDO.setDataScopeRule(deptDTO.getDataScopeRule());
        updatedDeptDO.setSort(deptDTO.getSort());
        // 假设修改人是admin
        updatedDeptDO.setUpdateBy("admin");
        updatedDeptDO.setUpdateTime(new Date());
        int result = deptMapper.updateById(updatedDeptDO);
        return result > 0;
    }

    @Override
    public SysDept getDeptInfo(DeptQuery deptQuery) {
        // 查询部门信息是否存在
        SysDept deptDO = deptMapper.selectById(deptQuery.getDeptId());
        if (deptDO == null) {
            throw new BusinessException(DEPT_NOT_FOUND);
        }
        // 查询部门信息
        return deptDO;
    }

    @Override
    public SysDept getDeptHierarchy(DeptQuery deptQuery) {
        // 查询部门层级关系
        QueryWrapper<SysDept> queryWrapper = Wrappers.query();
        if (deptQuery.getAncestorDeptId() != null) {
            queryWrapper.eq("ancestor_dept_id", deptQuery.getAncestorDeptId());
        }
        if (deptQuery.getDescendantDeptId() != null) {
            queryWrapper.eq("descendant_dept_id", deptQuery.getDescendantDeptId());
        }
        if (deptQuery.getDepth() != null) {
            queryWrapper.eq("depth", deptQuery.getDepth());
        }
        SysDept deptDO = deptMapper.selectOne(queryWrapper);
        return deptDO;
    }
}
